package com.ruoyi.shop.clientuser.address.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.common.domain.DtsAddress;
import com.ruoyi.mall.common.domain.DtsUser;
import com.ruoyi.mall.common.mapper.DtsAddressMapper;
import com.ruoyi.mall.common.mapper.DtsUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.clientuser.address.service.IDtsAddressService;

/**
 * 收货地址Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class DtsAddressServiceImpl implements IDtsAddressService
{
    @Autowired
    private DtsAddressMapper dtsAddressMapper;

    @Autowired
    private DtsUserMapper dtsUserMapper;

    /**
     * 查询收货地址
     *
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public DtsAddress selectDtsAddressById(Long id)
    {
        return dtsAddressMapper.selectDtsAddressById(id);
    }

    /**
     * 查询收货地址列表
     *
     * @param dtsAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<DtsAddress> selectDtsAddressList(DtsAddress dtsAddress)
    {
        // 用户列表
        List<DtsUser> dtsUserList1 = new ArrayList<>();
        // 需要返回的地址
        List<DtsAddress> dtsAddressList = new ArrayList<>();

        // 获取查询信息
        DtsUser dtsUser1 = new DtsUser();
        dtsUser1.setUsername(dtsAddress.getUsername());
        dtsUser1.setNickname(dtsAddress.getNickname());

        // 查询所有用户
        List<DtsUser> dtsUserList = dtsUserMapper.selectAddList(dtsUser1);
        // 加入收藏
        DtsAddress dtsAddress1 = new DtsAddress();
        // 查询有收藏用户总数
        int count = dtsUserMapper.selectAddCount(dtsUser1);

        // 循环用户
        for (DtsUser dtsUser : dtsUserList){
            // 使用用户id查询收藏
            dtsAddress.setUserId(dtsUser.getId());
            // 查询地址
            List<DtsAddress> dtsAddress2 = dtsAddressMapper.selectDtsAddressList(dtsAddress);
            // 判断是否有地址
            if (dtsAddress2.size() == 0) {
                // 没有则下一次
                continue;
            }
            // 有则加入用户中的收藏列表
            dtsUser.setDtsAddressList(dtsAddress2);
            dtsUserList1.add(dtsUser);
        }
        dtsAddress1.setDtsUserList(dtsUserList1);
        dtsAddress1.setCount(count);
        dtsAddressList.add(dtsAddress1);
        return dtsAddressList;
    }

    /**
     * 新增收货地址
     *
     * @param dtsAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertDtsAddress(DtsAddress dtsAddress)
    {
        return dtsAddressMapper.insertDtsAddress(dtsAddress);
    }

    /**
     * 修改收货地址
     *
     * @param dtsAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateDtsAddress(DtsAddress dtsAddress)
    {
        dtsAddress.setUpdateTime(DateUtils.getNowDate());
        return dtsAddressMapper.updateDtsAddress(dtsAddress);
    }

    /**
     * 批量删除收货地址
     *
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteDtsAddressByIds(Long[] ids)
    {
        return dtsAddressMapper.deleteDtsAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteDtsAddressById(Long id)
    {
        return dtsAddressMapper.deleteDtsAddressById(id);
    }
}
